package com.example.hibernate.demo.controller;

import com.example.hibernate.demo.entity.Employee;
import com.example.hibernate.demo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/employees")
@RequiredArgsConstructor
public class EmployeeController {
    
    private final EmployeeService employeeService;
    
    @GetMapping
    public ResponseEntity<List<Employee>> getAllEmployees() {
        return ResponseEntity.ok(employeeService.findAll());
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Employee> getEmployeeById(@PathVariable Long id) {
        return ResponseEntity.ok(employeeService.findById(id));
    }
    
    @GetMapping("/email/{email}")
    public ResponseEntity<Employee> getEmployeeByEmail(@PathVariable String email) {
        return ResponseEntity.ok(employeeService.findByEmail(email));
    }
    
    @GetMapping("/department/{departmentId}")
    public ResponseEntity<List<Employee>> getEmployeesByDepartment(@PathVariable Long departmentId) {
        return ResponseEntity.ok(employeeService.findByDepartmentId(departmentId));
    }
    
    @GetMapping("/salary")
    public ResponseEntity<List<Employee>> getEmployeesBySalaryRange(
            @RequestParam BigDecimal min, @RequestParam BigDecimal max) {
        return ResponseEntity.ok(employeeService.findBySalaryRange(min, max));
    }
    
    @GetMapping("/status/{status}")
    public ResponseEntity<Page<Employee>> getEmployeesByStatus(
            @PathVariable Employee.EmployeeStatus status, Pageable pageable) {
        return ResponseEntity.ok(employeeService.findByStatus(status, pageable));
    }
    
    @PostMapping
    public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
        return new ResponseEntity<>(employeeService.save(employee), HttpStatus.CREATED);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
        return ResponseEntity.ok(employeeService.update(id, employee));
    }
    
    @PostMapping("/{employeeId}/projects/{projectId}")
    public ResponseEntity<Employee> assignToProject(
            @PathVariable Long employeeId, @PathVariable Long projectId) {
        return ResponseEntity.ok(employeeService.assignToProject(employeeId, projectId));
    }
    
    @DeleteMapping("/{employeeId}/projects/{projectId}")
    public ResponseEntity<Employee> removeFromProject(
            @PathVariable Long employeeId, @PathVariable Long projectId) {
        return ResponseEntity.ok(employeeService.removeFromProject(employeeId, projectId));
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteEmployee(@PathVariable Long id) {
        employeeService.delete(id);
        return ResponseEntity.noContent().build();
    }
}